/*
 *
 * 名称：五运六气窗口
 * 功能：显示五运六气圆盘、细节、四柱属性等
 * 作者：清河张
 * 时间：2025.05.11
 *
 */

#include "wylqwin.h"
#include "ui_wylqwin.h"
#include <QGraphicsRotation>
#include <QGraphicsPathItem>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QtMath>
#include <QDebug>
#include "base/xzqm.h"
#include "base/gua8.h"

using namespace std;



WYLQWIN::WYLQWIN(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::WYLQWIN)
{
    ui->setupUi(this);
    QGraphicsScene* ssring = new QGraphicsScene(this);
    _ssblk = new QGraphicsScene(this);

    ssring->setSceneRect(0,0,560,560);
    _ssblk->setBackgroundBrush(Qt::gray);
    _ssblk->setSceneRect(0,0,160,160);



    ui->qkGraphicsView->setScene(ssring);
    _lp = new LUOPAN(ssring);
    ui->qkGraphicsView->setRenderHint(QPainter::Antialiasing);
    ui->qkGraphicsView->setAlignment(Qt::AlignCenter);

    ui->blockGraphicsView->setScene(_ssblk);

    ui->sizhuTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->sizhuTableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->wuyunliuqiTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->wuyunliuqiTableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    init();
}

WYLQWIN::~WYLQWIN()
{
    delete _lp;
    _lp = nullptr;
    delete ui;
}

void WYLQWIN::init()
{
    qreal startRadians;

    startRadians = divToRadians( - 16.0 );
    LUOPAN::SHENFU xtgattr{startRadians,1,1,0,{},{}};
    for(int i = 0; i < 8; ++i)
        xtgattr._strs.push_back( gong9_xu_gong(i)->gua1().name() );
    xtgattr._tishi.resize(1);
    _shenpan[SHEN_XTGUA] = xtgattr;


    startRadians = divToRadians( - 20.0 );
    LUOPAN::SHENFU tgattr{startRadians,1,1,0,{},{}};
    GAN10::push_back_name(tgattr._strs);
    tgattr._tishi.resize(2);
    _shenpan[SHEN_TIANGAN] = tgattr;


    LUOPAN::SHENFU syattr{startRadians,1,2,0,{},{}};
    for(int i = 0; i < 10; ++i)
        syattr._strs.push_back(WUYUNLIUQI::suiyun(i));
    syattr._tishi.resize(1);
    _shenpan[SHEN_SUIYUN] = syattr;


    startRadians = divToRadians( - 24.0 );
    LUOPAN::SHENFU dzattr{startRadians,0,1,0,{},{}};
    ZHI12::push_back_name(dzattr._strs);
    dzattr._tishi.resize(2);
    _shenpan[SHEN_DIZHI] = dzattr;



    LUOPAN::SHENFU sqattr {startRadians,0,4,0,{},{}};
    for(int i = 0; i < 12; ++i)
        sqattr._strs.push_back( WUYUNLIUQI::suiqi(i));
    sqattr._tishi.resize(1);
    _shenpan[SHEN_SUIQI] = sqattr;


    LUOPAN::SHENFU zyattr{0,1,2,0,{},{}};
    zyattr._strs.resize(5);
    zyattr._tishi.resize(1);
    _shenpan[SHEN_ZHUYUN] = zyattr;

    LUOPAN::SHENFU kyattr{0,1,2,0,{},{}};
    kyattr._strs.resize(5);
    kyattr._tishi.resize(1);
    _shenpan[SHEN_KEYUN] = kyattr;

    LUOPAN::SHENFU  zqattr {0,0,4,0,{},{}};
    zqattr._strs.resize(6);
    zqattr._tishi.resize(1);
    _shenpan[SHEN_ZHUQI] = zqattr;

    LUOPAN::SHENFU  kqattr {0,0,4,0,{},{}};
    kqattr._strs.resize(6);
    kqattr._tishi.resize(1);
    _shenpan[SHEN_KEQI] = kqattr;


    LUOPAN::SHENFU jqattr{0,1,2,0,{},{}};
    JIEQI::push_back_name(jqattr._strs);
    jqattr._tishi.resize(1);
    _shenpan[SHEN_JIEQI] = jqattr;

}


void WYLQWIN::display(const QDateTime &dateTime1, const QDateTime &dateTime2)
{
    LIFA lf1 = LIFA::fromgl(dateTime1);
    LIFA lf2 = LIFA::fromgl(dateTime2);
    WUYUNLIUQI wylq1(lf1);
    // WUYUNLIUQI wylq2(lf2);
    dingbiaoqian(lf1, wylq1);
    tianbiao(lf1, wylq1);
    _lp ->huashen(_shenpan);
    huasekuai(wylq1);
    // ui->showTextBrowser->setText();
    ui->statusbar->showMessage(
        lf1.dateTime().toString("使用公元时间：yyyy-MM-dd HH:mm:ss ") +
        lf2.dateTime().toString("诊断公元时间：yyyy-MM-dd HH:mm:ss ") );
}


void WYLQWIN::dingbiaoqian(const LIFA &lf, WUYUNLIUQI& wylq)
{
    const GANZHI suigz = GANZHI::gz60(lf.sui360());
    const BAZI& bz = lf.bazi();
    qint64 chenmiao;//时辰内秒数
    qreal jqbi, miaobi, startangle, offsetangle;

    jqbi = lf.jiefen() / 24.0;

    chenmiao = ( QTime(0,0,0).secsTo(lf.tm()) + 60*60 ) % (60*60*2);
    miaobi = qreal(chenmiao) / (60*60*2);

    LUOPAN::SHENFU& xtgattr = _shenpan[SHEN_XTGUA];
    xtgattr._tishi[0]._offsetRadians = ratioToRadians(jqbi) - xtgattr._startRadians;


    LUOPAN::SHENFU& tgattr = _shenpan[SHEN_TIANGAN];
    offsetangle = ratioToRadians( ( miaobi + bz._s._g.id() ) / 10.0 );
    tgattr._tishi[0]._offsetRadians = offsetangle;
    tgattr._tishi[0]._name = "时";

    offsetangle = ratioToRadians( ( jqbi + suigz._g.id() ) / 10.0 );
    tgattr._tishi[1]._offsetRadians = offsetangle;
    tgattr._tishi[1]._name = "岁";


    LUOPAN::SHENFU& syattr= _shenpan[SHEN_SUIYUN];
    syattr._tishi[0]._offsetRadians = offsetangle;
    syattr._tishi[0]._name = "岁运";


    offsetangle = ratioToRadians( ( miaobi + bz._s._z.id() ) / 12.0 );
    LUOPAN::SHENFU& dzattr= _shenpan[SHEN_DIZHI];
    dzattr._tishi[0]._offsetRadians = offsetangle;
    dzattr._tishi[0]._name = "时";

    offsetangle = ratioToRadians( ( jqbi + suigz._z.id() ) / 12.0 );
    dzattr._tishi[1]._offsetRadians = offsetangle;
    dzattr._tishi[1]._name = "岁";

    LUOPAN::SHENFU& sqattr = _shenpan[SHEN_SUIQI];
    sqattr._tishi[0]._offsetRadians = offsetangle;
    sqattr._tishi[0]._name = "岁气";


    startangle = ratioToRadians(wylq.jszhoulv());
    offsetangle = ratioToRadians(wylq.yun5r() / 5.0);
    LUOPAN::SHENFU& zyattr= _shenpan[SHEN_ZHUYUN];
    zyattr._startRadians = startangle;
    zyattr._tishi[0]._offsetRadians = offsetangle;
    zyattr._tishi[0]._name = "主运";

    for(size_t i = 0; i < zyattr._strs.size(); ++i)
        zyattr._strs[i] = wylq.zhuyun(i);


    LUOPAN::SHENFU& kyattr= _shenpan[SHEN_KEYUN];
    kyattr._startRadians = startangle;
    kyattr._tishi[0]._offsetRadians = offsetangle;
    kyattr._tishi[0]._name = "客运";

    for(size_t i = 0; i < kyattr._strs.size(); ++i)
        kyattr._strs[i] = wylq.keyun(i);


    startangle = ratioToRadians( wylq.jszhoulv() );
    offsetangle = ratioToRadians( wylq.qi6r() / 6.0 );

    LUOPAN::SHENFU& zqattr = _shenpan[SHEN_ZHUQI];
    zqattr._startRadians = startangle;
    zqattr._tishi[0]._offsetRadians = offsetangle;
    zqattr._tishi[0]._name = "主气";

    for(int i = 0; i < 6; ++i)
        zqattr._strs[i] = wylq.zhuqi(i);


    LUOPAN::SHENFU& kqattr= _shenpan[SHEN_KEQI];
    kqattr._startRadians = startangle;
    kqattr._tishi[0]._offsetRadians = offsetangle;
    kqattr._tishi[0]._name = "客气";
    for(int i = 0; i < 6; ++i)
        kqattr._strs[i] = wylq.keqi(i);


    LUOPAN::SHENFU::TISHI& jqtishi= _shenpan[SHEN_JIEQI]._tishi[0];
    jqtishi._offsetRadians = ratioToRadians(jqbi);
    jqtishi._name = "节气";

}

void WYLQWIN::tianbiao(const LIFA &lf, const WUYUNLIUQI& wylq)
{
    const BAZI& bz = lf.bazi();
    int row = 0, col = 0;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.wx5().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.wx5().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.wx5().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.wx5().name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.chong().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.chong().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.chong().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.chong().name());
    row++;

    col = 0;
    ui->sizhuTableWidget->item(row, col++)->setText(bz._n.he().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._y.he().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._r.he().name());
    ui->sizhuTableWidget->item(row, col++)->setText(bz._s.he().name());
    row++;

    row = 0;
    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.suiyun() );//大运
    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.zhuyun() );//主运
    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.keyun() );//客运

    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.sitian() );//司天
    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.zaiquan() );//在泉

    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.zhuqi() );//主气
    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.keqi());//客气
    ui->wuyunliuqiTableWidget->item(row++, 0)->setText( wylq.jialin());//加临

    ui->wuyunliuqiTableWidget->item(row++, 0)->setText(
        bz._s._z.jingluo());

}

void WYLQWIN::huasekuai(const WUYUNLIUQI &wylq)
{
    int ret;
    QGraphicsTextItem *textItem;
    QFont font("宋体");
    QPen pen(Qt::blue);
    Qt::GlobalColor eclr = Qt::gray;
    qreal sw, sh, x, y, h, fz;
    WUXING5 wx;
    YY yy;
    YY6QI6 yy6qi;
    _ssblk->clear();
    sw = _ssblk->sceneRect().width();
    sh = _ssblk->sceneRect().height();
    fz = sh / 16;
    h = sh / 16;
    font.setPixelSize( fz );

    //岁运
    wylq.suiyun(&wx,&yy);
    eclr = wyclr(wx.id());
    y = h * 4;
    x = yy ? 0 : sw / 4;

    _ssblk->addRect(x, y, sw - x * 2, h, pen, eclr);

    //主运
    wylq.zhuyun(&wx,&yy);
    eclr = wyclr(wx.id());
    y -= h;
    x = yy ? 0 : sw / 4;

    _ssblk->addRect(x, y, sw - x * 2, h, pen, eclr);

    //客运
    wylq.keyun(&wx,&yy);
    eclr = wyclr(wx.id());
    y -= h;
    x = yy ? 0 : sw / 4;
    _ssblk->addRect(x, y, sw - x * 2, h, pen, eclr);


    textItem = _ssblk->addText("五运：岁运 主运 客运",font);
    textItem->setDefaultTextColor(Qt::white);
    textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
    textItem->setPos(0, h * 6);

    x = sw / 8;

    //司在
    ret = wylq.sizai(yy6qi);
    eclr = lqclr(yy6qi.id());
    y = h * 13;
    _ssblk->addRect(x, y, sw - x * 2, h, pen, eclr);

    //主气
    wylq.zhuqi(yy6qi);
    eclr = lqclr(yy6qi.id());
    y -= h;
    _ssblk->addRect(x, y, sw - x * 2, h, pen, eclr);


    //客气
    wylq.keqi(yy6qi);
    eclr = lqclr(yy6qi.id());
    y -= h;
    _ssblk->addRect(x, y, sw - x * 2, h, pen, eclr);

    if(ret)
        textItem = _ssblk->addText("六气：在泉 主气 客气", font);
    else
        textItem = _ssblk->addText("六气：司天 主气 客气", font);
    textItem->setDefaultTextColor(Qt::white);
    textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
    textItem->setPos(0, h * 14);

}

Qt::GlobalColor WYLQWIN::wyclr(int wy)
{
    return (Qt::GlobalColor[]){ Qt::green, Qt::red, Qt::yellow, Qt::white, Qt::black}[wy%5];
}

Qt::GlobalColor WYLQWIN::lqclr(int lq)
{
    return (Qt::GlobalColor[]){ Qt::green, Qt::red, Qt::darkRed, Qt::yellow, Qt::white, Qt::black}[lq%6];
}


